<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Branchoffice extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->template->set_partial('header_search', 'header/header_search');
		$this->template->set_partial('nav_bar', 'header/nav_bar');
		$this->load->model('member/member_model');
		$this->load->model('member/profile_model');
		$this->load->model('member/qualification_model');
		$this->load->model('member/license_model');
		$this->load->model('member/association_model');
	}
	
	function index()
	{
		$data['mode'] = "ADD";
		/*GET PROFILE ID*/
		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);
		
		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile_id'] = $profile_id;
			$data['profile']['info'] = $isProfile;
			$data['association']['details'] = $this->association_model->getAssociations($profile_id);
		
		}else{
			$data['profile_id'] = null;
		}
		/*GET PROFILE ID*/
		
		
		
		$this->template->set_layout('full_layout');
		$data['module_banner'] = FALSE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = FALSE;
		
		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}
		
		$this->template->build('branchoffice',$data);
	}
	
	function edit($id)
	{
		$data['mode'] = "EDIT";
	
		/*GET PROFILE ID*/
		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);
	
		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile_id'] = $profile_id;
			$data['profile']['info'] = $isProfile;
		}else{
			$data['profile_id'] = null;
		}
		/*GET PROFILE ID*/
	
		$data['association']['info'] = $this->association_model->getAssociationsinfo($id);
	
		$this->template->set_layout('full_layout');
		$data['module_banner'] = FALSE;
		$data['module_recent_listing'] = TRUE;
		$data['module_news'] = FALSE;
	
		if($this->member_model->getMember($this->session->userdata['mem_id'])){
			$data['member']['info'] = $this->member_model->getMember($this->session->userdata['mem_id']);
		}
	
		$this->template->build('association',$data);
	
	}
	
	function submit_association()
	{
	
		/*GET PROFILE ID*/
		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);
	
		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile_id'] = $profile_id;
			$data['profile']['info'] = $isProfile;
		}else{
			$data['profile_id'] = null;
		}
	
		$this->form_validation->set_rules('position', 'Position', 'required|xss_clean');
		$this->form_validation->set_rules('association', 'Association', 'required|xss_clean ');
		$this->form_validation->set_rules('year_from', 'From Year', 'required|xss_clean');
		$this->form_validation->set_rules('year_to', 'To Year', 'required|xss_clean');
		
		if ($this->form_validation->run() == true) {
	
	
			$profile_id = $profile_id;
	
			$post_data=array(
	
					'b_name' => $this->input->post('position'),
					'b_address' => $this->input->post('association'),
					'b_phone' => $this->input->post('year_from'),
					
	
			);
			
			
			
			$this->db->insert('association',$post_data);
			$this->session->set_flashdata('message', 'Sucessfully!... ');
			redirect('member/association');
	
		} else {
			$this->session->set_flashdata('message', 'Invalid');
			$this->index();
		}
	}
	
	function update_association()
	{
		/*GET PROFILE ID*/
		$isProfile  = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id']);
	
		if($isProfile){
			$profile_id = $this->profile_model->getProfileByMemberID($this->session->userdata['mem_id'])->id;
			$data['profile_id'] = $profile_id;
			$data['profile']['info'] = $isProfile;
				
		}else{
			$data['profile_id'] = null;
		}
	
		$this->form_validation->set_rules('position', 'Position', 'required|xss_clean');
		$this->form_validation->set_rules('association', 'Association', 'required|xss_clean ');
		$this->form_validation->set_rules('year_from', 'From Year', 'required|xss_clean');
		$this->form_validation->set_rules('year_to', 'To Year', 'required|xss_clean');
		if ($this->form_validation->run() == true) {
	
			$id = $this->input->post('id');
				
			$post_data=array(
	
					//'profile_id' =>$profile_id,
					'position' => $this->input->post('position'),
					'association' => $this->input->post('association'),
					'year_from' => $this->input->post('year_from'),
					'year_to' => $this->input->post('year_to'),
					
			);
	
			$this->db->where('id',$id);
			$this->db->update('association',$post_data);
			$this->session->set_flashdata('message', 'Update Sucessfully!... ');
			redirect('member/association/edit/'.$id);
	
		} else {
			$this->session->set_flashdata('message', 'Invalid');
			redirect('member/association/edit/'.$id);
		}
	}
	
	
	
	function delete($id)
	{
		$this->association_model->deleteAssociation($id);
		redirect('member/association');
	}
}